package com.kevin.action.back;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.mvc.view.JspView;

import com.kevin.common.DwzoutMapUtil;
import com.kevin.model.Article;
import com.kevin.model.selector.ArticleColor;
import com.kevin.service.ArticleInfoService;
import com.kevin.validate.Errors;


/**
 * 
  * @ClassName: ArticleInfoBackAction 
  * @Description: 后台文章模块
  * @author 吕凯
  * @date 2012-10-29 上午10:18:54 
  *
 */
@IocBean
@At("/admin/article")
@Filters( { @By(type = CheckSession.class, args = { "admin", "/admin/loginp.shtml" }) })
public class ArticleBackAction {
	@Inject
	protected ArticleInfoService articleInfoService;
	
	/**
	 * 获取文章列表
	 * 
	 * @return
	 */
	@At
	public View showArticleInfoList(@Param("pageNum")int currentPage,HttpServletRequest request) {
		if(currentPage == 0)
			currentPage = 1;
		Integer pageSize = 10;
		try {
			pageSize = Integer.parseInt(request.getParameter("numPerPage"));
		} catch (NumberFormatException e) {}
		SqlExpressionGroup group = Cnd.exps("1", "=", "1");

		String title = request.getParameter("title");
		if (title != null && !title.equals("")) {
			group = group.and("title", "like", "%"+title + "%");
		}
		String addDate = request.getParameter("addDate");
		if (addDate != null && !addDate.equals("")) {
			group = group.and("createDate", "like", "%" + addDate + "%");
		}
		articleInfoService.getArticleList(currentPage,pageSize,request,Cnd.where(group).desc("modifyDate"));
		return new JspView("/jsp/back/article/showArticleInfoList");
	}
	/**
	 * 获取文章信息
	 * 
	 * @return
	 */
	@At
	@Ok("jsp:/jsp/article/showArticleInfo")
	public View showArticleInfo(@Param("id")int id,HttpServletRequest request) {
		articleInfoService.getArticle(id, request);
		request.setAttribute("artcleColors", ArticleColor.values());
		String edit = request.getParameter("e")+"";
		//编辑
		if(edit.equals("1")){
			return new JspView("/jsp/back/article/editArticleInfo");
		}
		//查看
		return new JspView("/jsp/back/article/showArticleInfo");
	}
	/**
	 * 添加文章
	 * 
	 * @return
	 */
	@At
	@Ok("jsp:/jsp/back/article/addArticleInfo")
	public void addArticleInfo(HttpServletRequest request) {
		request.setAttribute("artcleColors", ArticleColor.values());
	}
	
	/**
	 * 增 ： http://localhost:8080/nutztest/articleinfo/addArticleInfoList.shtml
	 */
	@At
	@Ok("json")
	@Aop("validationInterceptor")   
	public Map<String,Object> addArticleInfoList(@Param("..") Article ai,@Param("navid") int[] ids,HttpServletRequest request,Errors es) {
		articleInfoService.addArticleInfoList(ai, ids,request, es);
		System.out.println(request.getParameter("navTabId"));
		Map<String,Object> returnMap =  DwzoutMapUtil.getSuccDwzoutMap("添加成功",request.getParameter("navTabId"),"","","","");
		return returnMap;
	}
	/**
	 * 
	  * @Title: editArticleInfo
	  * @Description: 修改
	  * @param @param ai
	  * @param @param request
	  * @param @param es
	  * @param @return    设定文件 
	  * @return     返回类型 
	  * @throws
	 */
	@At
	//@Ok("redirect:/admin/articleinfo/showArticleInfoList.shtml")
	@Ok("json")
	@Aop("validationInterceptor")   
	public Map<String,Object> editArticleInfo(@Param("..") Article ai,@Param("navid") int[] ids,HttpServletRequest request,Errors es) {
		articleInfoService.editArticle(ai,ids, request, es);
		Map<String,Object> returnMap =  DwzoutMapUtil.getDwzoutMap(DwzoutMapUtil.SUCCESS, "修改成功", request.getParameter("navTabId"), "", "", "", "");
		return returnMap;
	}
}
